package com.itheima.appserver.controller;

import cn.hutool.core.convert.Convert;
import com.itheima.appserver.service.TestSoulService;
import com.itheima.commons.model.vo.CharacterVo;
import com.itheima.commons.model.vo.PostSubjectOptionVo;
import com.itheima.commons.model.vo.QuestionnaireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 测灵魂
 */
@RestController
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 测灵魂的问卷调查
     * @return
     */
    @GetMapping()
    public ResponseEntity questionnaireList() {
        List<QuestionnaireVo> questionnaireVo = testSoulService.getQuestionnaire();
        return ResponseEntity.ok(questionnaireVo);
    }

    /**
     * 测灵魂的查看结果
     * @return
     */
    //@PostMapping("fans/{uid}") 获取@PathVariable("uid") Long userId//Path中
    @GetMapping("/report/{id}")
    //@PathVariable("id") String id
    public ResponseEntity viewResults(@PathVariable("id") String id) {
        CharacterVo characterVo = testSoulService.viewResults(Convert.toLong(id));
        return ResponseEntity.ok(characterVo);
    }

    /**[PostSubjectOptionVo(questionId=1, optionId=1),
     *  PostSubjectOptionVo(questionId=2, optionId=5),
     *  PostSubjectOptionVo(questionId=3, optionId=11),
     *  PostSubjectOptionVo(questionId=4, optionId=15)]
     * 测灵魂的提交问卷
     * @return
     */
    @PostMapping()
    public ResponseEntity submitQuestionnaire(@RequestBody Map<String, List<PostSubjectOptionVo>> parm) {
        //获取
        List<PostSubjectOptionVo> answers = parm.get("answers");
        //将用户答题情况存入数据库
        String id = testSoulService.saveResults(answers);

        return ResponseEntity.ok(id);
    }


}
